The Art of WebAssembly by Rick Battagline
Author:Rick Battagline [Rick Battagline]
Language: eng
Format: epub, azw3
Publisher: No Starch Press
Published: 2021-05-31T16:00:00+00:00
Collision Detection Function
In other sections, weâve defined the WAT code before the JavaScript. In this section, the JavaScript initializes the values that define the circles in the array of structures. For that reason, weâll be writing the JavaScript first in this section. When youâre doing collision detection between two circles, you use the Pythagorean theorem to determine whether the distance between the centers of your circles is greater than the sum of the circlesâ radii. The WAT code in this section loops over each of the circles weâve defined in the WebAssembly memory, comparing it to every other circle to see whether they collide. The details of collision detection arenât the focus of this section, so we wonât go into it too deeply. Itâs simply a means to demonstrate how you can separate your data into structures and use that data to perform computations with your WAT code.
The first portion of the WAT code defines the imports from the JavaScript. Listing 6-19 shows the beginning of the WAT module.
data_structures.wat (part 1 of 6)
(module (import "env" "mem" (memory 1)) 1 (global $obj_base_addr (import "env" "obj_base_addr") i32) 2 (global $obj_count (import "env" "obj_count") i32) 3 (global $obj_stride (import "env" "obj_stride") i32) ;; attribute offset locations 4 (global $x_offset (import "env" "x_offset") i32) 5 (global $y_offset (import "env" "y_offset") i32) 6 (global $radius_offset (import "env" "radius_offset") i32) 7 (global $collision_offset (import "env" "collision_offset") i32) ...
Listing 6-19: Importing global variables that define the data structure
The global variables passed into the WebAssembly module define the layout of the linear memory and the data structures within it. The $obj_base_addr 1 global variable is the location in memory where the circle structures are defined. The $obj_count 2 global variable is the number of circles defined in linear memory. The $obj_stride 3 global variable is the number of bytes between each of the circle definitions. Then we import values for each of the attributes. The $x_offset 4, $y_offset 5, $radius_offset 6, and $collision_offset 7 are the number of bytes between the start of the objectâs x, y, radius, and collision flag values. These must be set inside this module.
Next, weâll define the $collision_check function. The details of how this function works are only valuable if youâre interested in how circle collision detection works. But as an overview, it uses the Pythagorean theorem to determine whether the distance between two circles is less than the sum of the circleâs radii. To briefly explain, letâs label the radius of the first circle R1, the radius of the second circle R2, and the distance between the circles D, as shown in Figure 6-6. No collision occurs if R1 + R2 is less than D.
Figure 6-6: There is no collision if R1 + R2 is less than the distance between the circles.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25282)
Hello! Python by Anthony Briggs(24335)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23421)
Kotlin in Action by Dmitry Jemerov(22504)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21965)
Dependency Injection in .NET by Mark Seemann(21838)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20707)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19520)
Grails in Action by Glen Smith Peter Ledbrook(18596)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17031)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13687)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11851)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11151)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10621)
Hit Refresh by Satya Nadella(9188)
The Kubernetes Operator Framework Book by Michael Dame(8563)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8400)
Robo-Advisor with Python by Aki Ranin(8359)